<template>
  <div class="login-page">
    <vaildateForm @form-submit="onFormSubmit">
      <div class="mb-3">
        <label for="exampleFormControlInput" class="form-label">邮箱地址</label>
        <!--        <input type="email" class="form-control" id="exampleFormControlInput1" v-model="emailRef.val" @blur="vaildateEmail">
        &lt;!&ndash;        提示信息&ndash;&gt;
                <div class="form-text" v-if="emailRef.error">{{ emailRef.message }}</div>-->
        <vaildate-input
          :rules="emailRules"
          placeholder="请输入邮箱地址"
          v-model="emailVal"
          @value="emailValue"
        ></vaildate-input>
      </div>
      <div class="mb-3">
        <label for="exampleFormControlInput" class="form-label">密码</label>
        <!--        <input type="password" class="form-control" >-->
        <vaildate-input
          placeholder="请输入密码"
          v-model="passwordVal"
          :rules="passwordRules"
          @value="passwordValue"
        ></vaildate-input>
      </div>
      <template #submit>
        <span class="btn btn-danger">登录</span>
      </template>
    </vaildateForm>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue";
import vaildateForm from "@/components/VaildateForm.vue";
import VaildateInput, { RulesProp } from "@/components/VaildateInput.vue";
import createMessage from "@/hooks/createMessage";
import { useStore } from "vuex";
import { useRouter } from "vue-router";

export default defineComponent({
  name: "LoginIndex",
  components: {
    vaildateForm,
    VaildateInput,
  },
  setup() {
    const emailVal = ref("111@test.com");
    const emailRules: RulesProp = [
      //这是邮箱的规则，应该被这个东西所约束
      { type: "required", message: "邮箱不能为空" }, //这是必填项
      { type: "email", message: "输入的邮箱格式不对" },
    ];
    const passwordVal = ref("111111");
    const passwordRules: RulesProp = [
      { type: "required", message: "密码不能为空" },
    ];
    console.log(emailVal.value);
    const emailValue = (val: string) => {
      emailVal.value = val;
      console.log(val);
    };
    const passwordValue = (val: string) => {
      passwordVal.value = val;
    };
    // 登录
    const store = useStore();
    const router = useRouter();
    const onFormSubmit = (result: boolean) => {
      console.log("123", result);
      if (result) {
        // console.log(1)

        const payload = {
          email: emailVal.value,
          password: passwordVal.value,
        };
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        store.dispatch("loginAndFecth", payload).then((data) => {
            createMessage("登录成功2秒后跳转到首页", "success");
            setTimeout(() => {
              router.push("/");
            }, 2000);
            // console.log(data);
            // console.log(store.state.token);
            // 发送登录
          })
          .catch((e) => {
            console.log(e);
          });
      }
    };
    return {
      emailRules,
      emailVal,
      emailValue,
      onFormSubmit,
      passwordVal,
      passwordRules,
      passwordValue,
    };
  },
});
</script>

<style>
.w-330 {
  max-width: 330px;
}</style>
